--[[
	节点，业务配置文件加载管理，reload
	当前主要只用了 load_config_node(),其他的还没有细看
]]
local skynet = require "skynet"
require "skynet.manager"
local filelog = require "filelog"
local configtool = require "configtool"
local configdao = require "configdao"

-- local cfgdatadao = require "cfgdatadao"
-- local lfstool = require "lfstool"

local filename = "configmng.lua"

local ConfigMng = {
	conf_node = nil,
}

local function load_config_node()
	--加载配置， 从config_node 中得到 nodename 并知道的 skynet的环境变量中去
	local config_node_path = skynet.getenv("node_config")
	ConfigMng.conf_node = configtool.load_config(config_node_path)
	skynet.setenv("nodename", ConfigMng.conf_node.nodename or "anonynode")
	-- 如果node节点配置了runtime 模式， 则用node的配置，否则用整个服务端的配置
	-- 这个时候 confshare 还没有启动，或者没有初始化，所以需要手工去获取config_svr文件中 runtime 的配置
	local runtime = "r"
	if ConfigMng.conf_node.runtime then 
		runtime = ConfigMng.conf_node.runtime
	else
		runtime = configtool.load_config(skynet.getenv("svrs_config"))["runtime"]
	end		
	skynet.setenv("runtime", runtime)
	if runtime == 'r' then
		skynet.error("APP run as Release")		
	elseif runtime == "d" then
		skynet.error("APP run as Debug")
	else
		skynet.error("APP runtime is error, ABORT")
		skynet.abort()
	end

	if ConfigMng.conf_node == nil then
		filelog.sys_error(filename," [BASIC_CONFIGMNG] ConfigMng:new load "..config_node_path.." failed")
		return false
	end	
	return true
end

local function load_allbusinesscfg()
	if ConfigMng.conf_node ~= nil then
		for _, value in pairs(ConfigMng.conf_node) do
			if value ~= nil and type(value) == "table" then
				for _, value1 in pairs(value) do
					if value1 ~= nil and type(value1) == "table" then
						for key2, value2 in pairs(value1) do
							if value2 ~= nil and type(value2) == "table" then
								if value2.cfgname ~= nil and type(value2.cfgname) == "string" and string.len(value2.cfgname) > 2 then
									local status = nil
									status, value2.conf, value2.changetime = cfgdatadao.query_datacfg_bycfgname(value2.cfgname)
									---filelog.sys_error("-----value2.cfgname-----",value2.cfgname, value2)
									if not status then
										for key3, value3 in pairs(value2) do
											if key3 ~= nil and key3 == "file" then
												value2.conf, value2.source = configtool.load_config(value3)
												-- local status, changetime = lfstool.get_file_changetime(value3)
												-- if status == true then
												-- 	value2.changetime = changetime
												-- end
											end
										end
									end
								else
									for key3, value3 in pairs(value2) do
										if key3 ~= nil and key3 == "file" then
											value2.conf, value2.source = configtool.load_config(value3)
											-- local status, changetime = lfstool.get_file_changetime(value3)
											-- if status == true then
											-- 	value2.changetime = changetime
											-- end
										end
									end
								end
							end
						end
					end
				end 
			end  
		end
	end
end

function ConfigMng.init()
	--加载节点配置
	if not load_config_node() then
		filelog.sys_error(filename, " [BASIC_CONFIGMNG] ConfigMng:new load cfgcenter failed")
		return nil
	end

	--加载所有的业务配置
	load_allbusinesscfg()
end

function ConfigMng.reload()
	--加载配置
	if not load_config_node() then
		filelog.sys_error(filename, " [BASIC_CONFIGMNG] ConfigMng:new load cfgcenter failed")
		return false
	end

	--加载所有的业务配置
	load_allbusinesscfg()
	return true	 
end

function ConfigMng.get_business_conf(platform, channel, business)
	if platform == nil or channel == nil or business == nil then
		filelog.sys_error(filename, " [BASIC_CONFIGMNG] COnfigMng:get_business_conf invalid param")		
		return nil
	end

	local platformtable = ConfigMng.conf_node[platform]
	if platformtable  == nil then
		filelog.sys_error(filename, " [BASIC_CONFIGMNG] COnfigMng:get_business_conf invalid platfrom:"..platform)		
		return nil
	end
	local channeltable = platformtable[channel]
	if channeltable == nil then
		filelog.sys_error(filename, " [BASIC_CONFIGMNG] COnfigMng:get_business_conf invalid channel:"..channel)		
		return nil		
	end
	local businesstable = channeltable[business]
	if businesstable == nil then
		filelog.sys_error(filename, " [BASIC_CONFIGMNG] COnfigMng:get_business_conf invalid business:"..business)		
		return nil		
	end
	return businesstable.conf, businesstable.changetime
end  

function ConfigMng.get_businessconfitem_by_index(platform, channel, business, index)
	local conf = ConfigMng.get_business_conf(platform, channel, business)

	if conf == nil then
		filelog.sys_error(filename, " [BASIC_CONFIGMNG] COnfigMng:get_businessconfitem_by_index get platform:"..platform.."channel:"..channel.." business:"..business.."failed")		
		return nil
	end

	return conf[index]
end

function ConfigMng.get_common_conf(conf_itemname)
	local conf = ConfigMng.conf_node[conf_itemname]
	if conf == nil then
		filelog.sys_error(filename, " [BASIC_CONFIGMNG] ConfigMng:get_common_conf get conf_itemname:"..conf_itemname)		
		return nil
	end

	return conf
end

function ConfigMng.get_cfgnode()
	return ConfigMng.conf_node
end 

return ConfigMng
